<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="https://fonts.cat.net/css?family=Lato:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">


  <link rel="manifest" href="/images/manifest.json">


  <meta name="msapplication-config" content="/images/browserconfig.xml" />



  <meta name="keywords" content="前端" />





  <link rel="alternate" href="/atom.xml" title="两万" type="application/atom+xml" />






<meta name="description" content="前端日常学习">
<meta name="keywords" content="js,html,css,node,javaScript,html5,css3,vue,canvas,前端,设计模式,后台,nginx">
<meta property="og:type" content="website">
<meta property="og:title" content="两万">
<meta property="og:url" content="http://blog.bevexed.top/page/2/index.html">
<meta property="og:site_name" content="两万">
<meta property="og:description" content="前端日常学习">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="两万">
<meta name="twitter:description" content="前端日常学习">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://blog.bevexed.top/page/2/"/>





  <title>两万 - Daily</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?4fdb363593835184af3e5b1a4874f9db";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">两万</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">Daily</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/08/19/前端设计模式（工厂模式）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/08/19/前端设计模式（工厂模式）/" itemprop="url">前端设计模式（工厂模式）</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-19T16:07:29+08:00">
                2019-08-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/设计模式/" itemprop="url" rel="index">
                    <span itemprop="name">设计模式</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/08/19/前端设计模式（工厂模式）/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/08/19/前端设计模式（工厂模式）/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/08/19/前端设计模式（工厂模式）/" class="leancloud_visitors" data-flag-title="前端设计模式（工厂模式）">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  127
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="工厂模式"><a href="#工厂模式" class="headerlink" title="工厂模式"></a>工厂模式</h2><ul>
<li>将 new 操作单独封装</li>
<li>遇到 new 时，可以考虑使用 工厂模式</li>
</ul>
<h2 id="demo"><a href="#demo" class="headerlink" title="demo"></a>demo</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Product</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>(name)&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name</span><br><span class="line">  &#125;</span><br><span class="line">  init()&#123;</span><br><span class="line">    </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Creator</span> </span>&#123;</span><br><span class="line">  create(name)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Product(name)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> creator = <span class="keyword">new</span> Creator()</span><br><span class="line"><span class="keyword">let</span> p = creator.create(<span class="string">'demo'</span>)</span><br><span class="line">p.init()</span><br></pre></td></tr></table></figure>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><ul>
<li><p>jQuery</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">jQuery</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>(selector)&#123;</span><br><span class="line">    <span class="keyword">let</span> slice = <span class="built_in">Array</span>.prototype.slice;</span><br><span class="line">    <span class="keyword">let</span> dom = slice.call(<span class="built_in">document</span>.querySelectorAll(selector));</span><br><span class="line">    <span class="keyword">let</span> len = dom ? dom.length : <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++)&#123;</span><br><span class="line">      <span class="keyword">this</span>[i] = dom[i]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.length = len;</span><br><span class="line">    <span class="keyword">this</span>.selector = selector || <span class="string">''</span>;</span><br><span class="line">  &#125;</span><br><span class="line">   append(node)&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">window</span>.$ = <span class="function"><span class="keyword">function</span>(<span class="params">selector</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> jQuery(selector)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>React.createElement</p>
</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/08/19/前端设计模式（设计原则）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/08/19/前端设计模式（设计原则）/" itemprop="url">前端设计模式（设计原则）</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-19T10:52:20+08:00">
                2019-08-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/设计模式/" itemprop="url" rel="index">
                    <span itemprop="name">设计模式</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/08/19/前端设计模式（设计原则）/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/08/19/前端设计模式（设计原则）/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/08/19/前端设计模式（设计原则）/" class="leancloud_visitors" data-flag-title="前端设计模式（设计原则）">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  619
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  2
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="何为设计"><a href="#何为设计" class="headerlink" title="何为设计"></a>何为设计</h2><ul>
<li>按照一种思路或标准来实现功能</li>
<li>功能相同，可以用不同的方案来实现</li>
<li><p>随着需求的增加，设计的作用才能体现出来</p>
<h3 id="《UNIX-LINUX设计哲学》"><a href="#《UNIX-LINUX设计哲学》" class="headerlink" title="《UNIX/LINUX设计哲学》"></a>《UNIX/LINUX设计哲学》</h3><ol>
<li>小即是美</li>
<li>让每个程序只做好一件事</li>
<li>快速建立原型</li>
<li>舍去高效率而取可移植性</li>
<li>采用纯文本来存储数据</li>
<li>充分利用软件的杠杆效应</li>
<li>使用 shell 脚本来提高杠杆效应和可移植性</li>
<li>避免强制性的用户界面</li>
<li>让每个程序都成为过滤器</li>
<li>允许用户定制环境</li>
<li>尽量使操作系统内核小而轻量化</li>
<li>使用小写字母并尽量简写</li>
<li>沉默是金</li>
<li>各部分之和大于整体</li>
<li>寻求百分之90的解决方案</li>
</ol>
<h3 id="如何学习"><a href="#如何学习" class="headerlink" title="如何学习"></a>如何学习</h3><ul>
<li>明白每个设计的道理和用意</li>
<li>通过经典应用体会他的真正使用场景</li>
<li>自己编码时多思考，尽量模仿（刻意训练）</li>
</ul>
</li>
</ul>
<h2 id="SOLID五大设计原则"><a href="#SOLID五大设计原则" class="headerlink" title="SOLID五大设计原则"></a>SOLID五大设计原则</h2><ul>
<li>S：单一职责原则</li>
<li>O：开放封闭原则</li>
<li>L：李氏置换原则</li>
<li>I：接口堵路原则</li>
<li><p>D：依赖倒置原则</p>
<h3 id="单一职责原则"><a href="#单一职责原则" class="headerlink" title="单一职责原则"></a>单一职责原则</h3><ul>
<li>一个程序只做好一件事</li>
<li>如果功能过于浮渣就拆分开，每个部分保持独立</li>
</ul>
<h3 id="开放封闭原则"><a href="#开放封闭原则" class="headerlink" title="开放封闭原则"></a>开放封闭原则</h3><ul>
<li>对扩展开放，对修改封闭</li>
<li>增加需求时，扩展新代码，而非修改已有代码</li>
<li>软件设计的终极目标</li>
</ul>
<h3 id="李氏置换原则"><a href="#李氏置换原则" class="headerlink" title="李氏置换原则"></a>李氏置换原则</h3><ul>
<li>子类能够覆盖父类</li>
<li>父类能处闲的地方子类就能出现</li>
<li>JS 种使用较少 </li>
</ul>
<h3 id="接口独立原则"><a href="#接口独立原则" class="headerlink" title="接口独立原则"></a>接口独立原则</h3><ul>
<li>保持接口的单一独立，避免出现“胖接口”</li>
</ul>
<h3 id="依赖倒置原则"><a href="#依赖倒置原则" class="headerlink" title="依赖倒置原则"></a>依赖倒置原则</h3><ul>
<li>面向接口编程，依赖于抽象而不依赖于具体</li>
<li>使用方只关注接口而不关注具体类的实现 </li>
</ul>
</li>
</ul>
<h2 id="从设计到模式"><a href="#从设计到模式" class="headerlink" title="从设计到模式"></a>从设计到模式</h2><h2 id="23种设计模式"><a href="#23种设计模式" class="headerlink" title="23种设计模式"></a>23种设计模式</h2><ul>
<li>创建型</li>
<li>组合型</li>
<li><p>行为型</p>
<h3 id="创建型"><a href="#创建型" class="headerlink" title="创建型"></a>创建型</h3><ul>
<li>工厂模式（工厂方法模式、抽象工厂模式、建造者模式）</li>
<li>单例模式</li>
<li>原型模式</li>
</ul>
<h3 id="结构型"><a href="#结构型" class="headerlink" title="结构型"></a>结构型</h3><ul>
<li>适配器模式</li>
<li>装饰器模式</li>
<li>代理模式</li>
<li>外观模式</li>
<li>桥接模式</li>
<li>组合模式</li>
<li>享元模式</li>
</ul>
<h3 id="行为型"><a href="#行为型" class="headerlink" title="行为型"></a>行为型</h3><ul>
<li>策略模式</li>
<li>模板方法模式</li>
<li>观察者模式（JS中用的多）</li>
<li>迭代器模式</li>
<li>职责链模式</li>
<li>命令模式</li>
<li>备忘录模式</li>
<li>状态模式</li>
<li>访问者模式</li>
<li>中介者模式</li>
<li>解释器模式</li>
</ul>
</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/07/30/webpack搭建简单环境/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/07/30/webpack搭建简单环境/" itemprop="url">webpack搭建简单环境</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-30T15:22:57+08:00">
                2019-07-30
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/构建工具/" itemprop="url" rel="index">
                    <span itemprop="name">构建工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/07/30/webpack搭建简单环境/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/07/30/webpack搭建简单环境/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/07/30/webpack搭建简单环境/" class="leancloud_visitors" data-flag-title="webpack搭建简单环境">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  219
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="初始化-npm"><a href="#初始化-npm" class="headerlink" title="初始化 npm"></a>初始化 npm</h2><blockquote>
<p>npm init</p>
</blockquote>
<h2 id="安装-webpack、webpack-cli"><a href="#安装-webpack、webpack-cli" class="headerlink" title="安装 webpack、webpack-cli"></a>安装 webpack、webpack-cli</h2><blockquote>
<p>npm i webpack webpack-cli -S</p>
</blockquote>
<h2 id="新建-webpack-dev-config-js"><a href="#新建-webpack-dev-config-js" class="headerlink" title="新建 webpack.dev.config.js"></a>新建 webpack.dev.config.js</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  entry: <span class="string">'./src/index.js'</span>,</span><br><span class="line">  output: &#123;</span><br><span class="line">    path: __dirname,</span><br><span class="line">    filename: <span class="string">"./release/bundle.js"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="新建-src-目录"><a href="#新建-src-目录" class="headerlink" title="新建 src 目录"></a>新建 src 目录</h2><ul>
<li>src<ul>
<li>index.js</li>
</ul>
</li>
<li>index.html</li>
</ul>
<h2 id="在-package-json-配置"><a href="#在-package-json-配置" class="headerlink" title="在 package.json 配置"></a>在 package.json 配置</h2><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"scripts"</span>: &#123;</span><br><span class="line">      <span class="attr">"test"</span>: <span class="string">""</span>,</span><br><span class="line">      <span class="attr">"dev"</span>: <span class="string">"webpack --config ./webpack.dev.config --mode development"</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="安装插件"><a href="#安装插件" class="headerlink" title="安装插件"></a>安装插件</h2><ul>
<li>webpack-dev-server </li>
<li>html-webpack-plugin<blockquote>
<p>npm i webpack-dev-server html-webpack-plugin -S</p>
</blockquote>
</li>
</ul>
<h2 id="修改-webpack-dev-config-js"><a href="#修改-webpack-dev-config-js" class="headerlink" title="修改 webpack.dev.config.js"></a>修改 webpack.dev.config.js</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">'html-webpack-plugin'</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  entry: <span class="string">'./src/index.js'</span>,</span><br><span class="line">  output: &#123;</span><br><span class="line">    path: __dirname,</span><br><span class="line">    filename: <span class="string">"./release/bundle.js"</span></span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  plugins: [</span><br><span class="line">    <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">      template: <span class="string">"./index.html"</span></span><br><span class="line">    &#125;)</span><br><span class="line">  ],</span><br><span class="line"></span><br><span class="line">  devServer: &#123;</span><br><span class="line">    contentBase: path.join(__dirname,<span class="string">'./release'</span>), <span class="comment">//根目录</span></span><br><span class="line">    open: <span class="literal">true</span>,</span><br><span class="line">    port: <span class="number">9000</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="修改-package-json"><a href="#修改-package-json" class="headerlink" title="修改 package.json"></a>修改 package.json</h2><blockquote>
<p>“dev”: “webpack-dev-server –config ./webpack.dev.config –mode development”</p>
</blockquote>
<h2 id="安装-babel-解析-ES6"><a href="#安装-babel-解析-ES6" class="headerlink" title="安装 babel 解析 ES6"></a>安装 babel 解析 ES6</h2><ul>
<li>babel-core</li>
<li>babel-loader</li>
<li>babel-polyfill</li>
<li>babel-preset-env</li>
<li>babel-preset-latest<blockquote>
<p>npm i babel-core babel-loader babel-polyfill babel-preset-env babel-preset-latest -S</p>
</blockquote>
</li>
</ul>
<h2 id="新建-babelrc"><a href="#新建-babelrc" class="headerlink" title="新建 .babelrc"></a>新建 .babelrc</h2><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"presets"</span>: [</span><br><span class="line">    <span class="string">"es2015"</span>,</span><br><span class="line">    <span class="string">"latest"</span></span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"plugins"</span>: []</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="在-webpack-dev-config-中配置-loader"><a href="#在-webpack-dev-config-中配置-loader" class="headerlink" title="在 webpack.dev.config 中配置 loader"></a>在 webpack.dev.config 中配置 loader</h2><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">module: &#123;</span><br><span class="line">    rules: [&#123;</span><br><span class="line">      test: /\.js?$/,</span><br><span class="line">      exclude: /(node_modules)/,</span><br><span class="line">      loader: 'babel-loader'</span><br><span class="line">    &#125;]</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/07/30/前端设计模式（面向对象）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/07/30/前端设计模式（面向对象）/" itemprop="url">前端设计模式（面向对象）</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-30T15:10:25+08:00">
                2019-07-30
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/设计模式/" itemprop="url" rel="index">
                    <span itemprop="name">设计模式</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/07/30/前端设计模式（面向对象）/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/07/30/前端设计模式（面向对象）/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/07/30/前端设计模式（面向对象）/" class="leancloud_visitors" data-flag-title="前端设计模式（面向对象）">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  318
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="设计模式"><a href="#设计模式" class="headerlink" title="设计模式"></a>设计模式</h2><ul>
<li>学会设计模式，可以帮你写更好的代码</li>
<li>编程应该 <strong>简单、抽象</strong></li>
</ul>
<h2 id="面向对象"><a href="#面向对象" class="headerlink" title="面向对象"></a>面向对象</h2><ul>
<li>数据结构化</li>
</ul>
<h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><ul>
<li>类：模板</li>
<li>对象：实例</li>
</ul>
<h3 id="三要素"><a href="#三要素" class="headerlink" title="三要素"></a>三要素</h3><ul>
<li>继承 <ul>
<li>继承可以将公共方法抽离出来，提高复用，减少冗余</li>
</ul>
</li>
<li>封装（ES6不支持，ts 支持）<ul>
<li>public 完全开放</li>
<li>protected 对子类开放</li>
<li>private 对自己开放</li>
<li>减少耦合，不该外露不外露</li>
<li>利于数据、接口的权限管理</li>
</ul>
</li>
<li>多态 <ul>
<li>同一个接口不同，不同表现</li>
<li>JS 应用极少</li>
<li>保持子类的开放性和灵活性</li>
<li>面向接口编程</li>
</ul>
</li>
</ul>
<h3 id="应用"><a href="#应用" class="headerlink" title="应用"></a>应用</h3><h4 id="jQuery"><a href="#jQuery" class="headerlink" title="jQuery"></a>jQuery</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">jQuery</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>(selector)&#123;</span><br><span class="line">    <span class="keyword">let</span> slice = <span class="built_in">Array</span>.prototype.slice;</span><br><span class="line">    <span class="keyword">let</span> dom = slice.call(<span class="built_in">document</span>.querySelectorAll(selector));</span><br><span class="line">    <span class="keyword">let</span> len = dom ? dom.length : <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++)&#123;</span><br><span class="line">      <span class="keyword">this</span>[i] = dom[i]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.length = len;</span><br><span class="line">    <span class="keyword">this</span>.selector = selector || <span class="string">''</span>;</span><br><span class="line">  &#125;</span><br><span class="line">   append(node)&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">window</span>.$ = <span class="function"><span class="keyword">function</span>(<span class="params">selector</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> jQuery(selector)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="demo"><a href="#demo" class="headerlink" title="demo"></a>demo</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>(name,age)&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name;</span><br><span class="line">    <span class="keyword">this</span>.age= age</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  getName()&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.name</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  eat()&#123;</span><br><span class="line">    alert(<span class="string">`<span class="subst">$&#123;<span class="keyword">this</span>.name&#125;</span> eat something`</span>)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  speak()&#123;</span><br><span class="line">    alert(<span class="string">`My name is <span class="subst">$&#123;<span class="keyword">this</span>.name&#125;</span>,age <span class="subst">$&#123;<span class="keyword">this</span>.age&#125;</span>`</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> p = <span class="keyword">new</span> Person(<span class="string">'webPack'</span>,<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">alert(p.getName())</span><br><span class="line"></span><br><span class="line">p.eat()</span><br><span class="line">p.speak()</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> <span class="keyword">extends</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>(name, age, number) &#123;</span><br><span class="line">    <span class="keyword">super</span>(name, age);</span><br><span class="line">    <span class="keyword">this</span>.number = number</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  study() &#123;</span><br><span class="line">    alert(<span class="string">`学号：<span class="subst">$&#123; <span class="keyword">this</span>.number &#125;</span>`</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> w = <span class="keyword">new</span> Student(<span class="string">'w'</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line">w.study();</span><br><span class="line">w.eat();</span><br><span class="line">w.speak();</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/07/16/TCP-IP/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/07/16/TCP-IP/" itemprop="url">TCP/IP</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-16T12:56:06+08:00">
                2019-07-16
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/网络基础/" itemprop="url" rel="index">
                    <span itemprop="name">网络基础</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/07/16/TCP-IP/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/07/16/TCP-IP/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/07/16/TCP-IP/" class="leancloud_visitors" data-flag-title="TCP/IP">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  329
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="分层"><a href="#分层" class="headerlink" title="分层"></a>分层</h1><ul>
<li>大多数的网络应用程序都被设计成了 <strong>客户端 - 服务器</strong> 模式</li>
<li>双方都有对应的 <strong>一或多个</strong> 协议进行通讯</li>
<li>链路层、网络层、运输层 一般在<strong>内核</strong>中执行，应用程序通常是用户进程</li>
<li>链路层、网络层、运输层 关注<strong>通讯</strong>细节</li>
<li>应用层 关注<strong>应用程序</strong></li>
<li>应用层和运输层使用端到端（end-to-end）协议<br>-</li>
</ul>
<h2 id="链路层（以太网-ARP-硬件接口）"><a href="#链路层（以太网-ARP-硬件接口）" class="headerlink" title="链路层（以太网/ARP/硬件接口）"></a>链路层（以太网/ARP/硬件接口）</h2><p>处理与电缆（或其他任何传输媒介）的物理接口细节</p>
<h2 id="网络层（IP-ICMP-IGMP）"><a href="#网络层（IP-ICMP-IGMP）" class="headerlink" title="网络层（IP/ICMP/IGMP）"></a>网络层（IP/ICMP/IGMP）</h2><p>处理分组在网络中的活动，例如分组选路</p>
<ul>
<li>提供逐跳协议（hop-to-hop）</li>
<li>网络ip提供的是一种不可靠的服务，他只是尽可能快的把分组从源结点送到目的结点，但不提供可靠性保障</li>
<li>ICMP 是 IP 协议的附属协议（告诉你出问题的原因）</li>
</ul>
<h3 id="ICMP"><a href="#ICMP" class="headerlink" title="ICMP"></a>ICMP</h3><h2 id="运输层（TCP-UDP）"><a href="#运输层（TCP-UDP）" class="headerlink" title="运输层（TCP/UDP）"></a>运输层（TCP/UDP）</h2><p>为两台主机上的应用程序提供端到端的通讯</p>
<ul>
<li>TCP 在不可靠的IP层上提供了一个可靠的运输层</li>
<li>UDP 不可靠</li>
</ul>
<h2 id="应用层（FTP）"><a href="#应用层（FTP）" class="headerlink" title="应用层（FTP）"></a>应用层（FTP）</h2><p>处理特定的应用程序细节</p>
<ul>
<li>互联网的目的之一就是在应用程序中隐藏所有的物理细节</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/06/13/二叉堆/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/13/二叉堆/" itemprop="url">二叉堆</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-13T13:55:15+08:00">
                2019-06-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/13/二叉堆/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/06/13/二叉堆/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/06/13/二叉堆/" class="leancloud_visitors" data-flag-title="二叉堆">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  349
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="二叉堆"><a href="#二叉堆" class="headerlink" title="二叉堆"></a>二叉堆</h1><ul>
<li>本质是一种 <strong>完全二叉树</strong></li>
<li>有 <strong>最小堆</strong> 和 <strong>最大堆</strong> 两种</li>
<li>二叉堆的根节点叫做 <strong>堆顶</strong></li>
<li>最大堆的堆顶是整个堆中的 <strong>最大元素</strong></li>
<li>最小堆的堆顶是整个堆中的 <strong>最小元素</strong></li>
</ul>
<h2 id="最大堆"><a href="#最大堆" class="headerlink" title="最大堆"></a>最大堆</h2><p>任何一个 <strong>父节点</strong> 的值，都 <strong>大于或等于</strong> 它左、右孩子节点的值</p>
<h2 id="最小堆"><a href="#最小堆" class="headerlink" title="最小堆"></a>最小堆</h2><p>任何一个 <strong>父节点</strong> 的值，都 <strong>小于或等于</strong> 它左、右孩子节点的值</p>
<h2 id="二叉堆的自我调整"><a href="#二叉堆的自我调整" class="headerlink" title="二叉堆的自我调整"></a>二叉堆的自我调整</h2><p>把一个不符合堆性质的完全二叉树，调整成一个堆，操作有</p>
<ul>
<li>插入节点</li>
<li>删除节点</li>
<li>构建二叉堆</li>
</ul>
<h3 id="插入节点"><a href="#插入节点" class="headerlink" title="插入节点"></a>插入节点</h3><ol>
<li>二叉堆插入节点时，插入位置是完全二叉树的 <strong>最后</strong> 一个位置</li>
<li>新节点通过与父节点比较位置和 <strong>上浮</strong> 来确认节点的位置</li>
</ol>
<h3 id="删除节点"><a href="#删除节点" class="headerlink" title="删除节点"></a>删除节点</h3><ol>
<li>二叉堆删除的是 <strong>堆顶</strong> 的节点</li>
<li>此时为了维持二叉堆的结构，需要用 <strong>最后一个</strong> 节点临时补充到堆顶的位置</li>
<li>通过 <strong>下沉</strong> 来确认节点的位置</li>
</ol>
<h3 id="构建二叉堆"><a href="#构建二叉堆" class="headerlink" title="构建二叉堆"></a>构建二叉堆</h3><ul>
<li>通过所有子叶 <strong>下沉</strong> 实现</li>
<li>下沉从最后一个节点开始</li>
<li>二叉堆的所有节点都存储在 <strong>数组</strong> 中</li>
<li>如果父节点的下标是 <strong>parent</strong>，则左孩子的下标为 <strong>2 * parent + 1</strong>，右孩子的下标为 <strong>2 * parent + 2</strong></li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/06/13/树/exercise/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/13/树/exercise/" itemprop="url">未命名</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-13T13:44:01+08:00">
                2019-06-13
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/13/树/exercise/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/06/13/树/exercise/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/06/13/树/exercise/" class="leancloud_visitors" data-flag-title="">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  550
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  2
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <!DOCTYPE html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <title>Binary Tree</title>
  <style>
    .root {
      display: flex;
      border: 1px solid #000;
      width: 600px;
      margin: 0 auto;
      height: 150px;
      align-items: center;
      justify-content: center;
    }

    .root div {
      display: flex;
      height: 70%;
      width: 44%;
      margin: 0 3%;
      border: 1px solid #000;
      justify-content: center;
      align-items: center;
      background: #fff;
    }
  </style>
</head>
<body>
<div class="root">
  1

  <div>2
    <div>4</div>
    <div>5</div>
  </div>

  <div>3
    <div>6</div>
    <div>7</div>
  </div>
</div>
</body>

<script>
  /**
   * 深度优先遍历的递归写法
   * 原理：递归的回溯性
   */
  class TravelTree {
    constructor() {
      this.nodeList = [];
    }

    preOrder = node => {
      if (node) {
        this.nodeList.push(node.firstChild.textContent.trim());
        this.preOrder(node.firstElementChild);
        this.preOrder(node.lastElementChild);
      }
    };

    inOrder = node => {
      if (node) {
        this.inOrder(node.firstElementChild);
        this.nodeList.push(node.firstChild.textContent.trim());
        this.inOrder(node.lastElementChild);
      }
    };

    postOrder = node => {
      if (node) {
        this.postOrder(node.firstElementChild);
        this.postOrder(node.lastElementChild);
        this.nodeList.push(node.firstChild.textContent.trim());
      }
    };

    reset() {
      this.nodeList = [];
    }

  }

  let node = document.querySelector('.root');
  let tree = new TravelTree();

  console.log('TravelTree');
  tree.reset();
  tree.preOrder(node);
  console.log(tree.nodeList);

  tree.reset();
  tree.inOrder(node);
  console.log(tree.nodeList);

  tree.reset();
  tree.postOrder(node);
  console.log(tree.nodeList);

  // 深度优先遍历的非递归写法
  class StackTree {
    constructor() {
      this.nodeList = [];
      this.stack = []
    }


    preOrder(node) {
      let treeNode = node;
      while (treeNode || this.stack.length) {
        // 进栈 将所有左节点压栈
        while (treeNode) {
          this.stack.push(treeNode);
          this.nodeList.push(treeNode.firstChild.textContent.trim());
          treeNode = treeNode.firstElementChild;
        }
        // 出栈
        if (this.stack.length) {
          treeNode = this.stack.pop();
          treeNode = treeNode.lastElementChild;
        }
      }
    }

    inOrder(node) {
      let treeNode = node;
      while (treeNode || this.stack.length) {
        while (treeNode) {
          this.stack.push(treeNode);
          treeNode = treeNode.firstElementChild;
        }
        if (this.stack.length) {
          treeNode = this.stack.pop();
          this.nodeList.push(treeNode.firstChild.textContent.trim());
          treeNode = treeNode.lastElementChild;
        }
      }
    }

    /*
    *  将根节点压入第一个栈
    *  从第一个栈中弹出一个元素，压入第二个栈
    *  然后分别将该节点的左右孩子压入第一个栈
    *  重复步骤2和步骤3直到第一个栈为空
    *  执行结束，第二个栈中就保存了所有节点的后序遍历输出结果。依次将元素从第二个栈中弹出即可。
    */

    postOrder(node) {
      let treeNode = node;
      this.stack.push(treeNode);
      while (this.stack.length) {
        treeNode = this.stack.pop();
        this.nodeList.unshift(treeNode.firstChild.textContent.trim());

        if (treeNode.firstElementChild) {
          this.stack.push(treeNode.firstElementChild)
        }
        if (treeNode.lastElementChild) {
          this.stack.push(treeNode.lastElementChild)
        }
      }
    }

    reset() {
      this.stack = [];
      this.nodeList = [];
    }

  }

  let stackTree = new StackTree();
  console.log('stackTree');

  stackTree.preOrder(node);
  console.log(stackTree.nodeList);

  stackTree.reset();
  stackTree.inOrder(node);
  console.log(stackTree.nodeList);

  stackTree.reset();
  stackTree.postOrder(node);
  console.log(stackTree.nodeList);

  // 广度度优先遍历非递归
  class WidthTravelTree {
    constructor() {
      this.nodeList = [];
      this.stack = []
    }

    // 队列
    widthTravel(node) {
      if (node) {
        this.stack.push(node);
        this.nodeList.push(node.firstChild.textContent.trim());
        while (this.stack.length) {
          let item = this.stack.shift();
          if (item.firstElementChild) {
            this.nodeList.push(item.firstElementChild.firstChild.textContent.trim());
            this.stack.push(item.firstElementChild);
          }
          if (item.lastElementChild) {
            this.nodeList.push(item.lastElementChild.firstChild.textContent.trim());
            this.stack.push(item.lastElementChild)
          }
        }
      }
    }
  }


  console.log('WidthTravelTree');
  let widthTravelTree = new WidthTravelTree();
  widthTravelTree.widthTravel(node);
  console.log(widthTravelTree.nodeList);

</script>
</html>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/06/10/树/demo/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/10/树/demo/" itemprop="url">未命名</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-10T17:21:29+08:00">
                2019-06-10
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/10/树/demo/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/06/10/树/demo/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/06/10/树/demo/" class="leancloud_visitors" data-flag-title="">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  276
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <!DOCTYPE html>
<html lang="">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .wrap {
      display: flex;
      border: 1px solid #000;
      width: 600px;
      margin: 0 auto;
      height: 150px;
      align-items: center;
      justify-content: center;
    }

    .wrap div {
      display: flex;
      height: 70%;
      width: 44%;
      margin: 0 3%;
      border: 1px solid #000;
      justify-content: center;
      align-items: center;
      background: #fff;
    }

    .btn-wrap {
      text-align: center;
      padding-top: 20px;
    }

    .btn-wrap button {
      display: inline-block;
      padding: 4px 10px;
    }

  </style>
</head>
<body>
<div class="wrap">
  <div>
    <div>
      <div>
        <div></div>
        <div></div>
      </div>
      <div>
        <div></div>
        <div></div>
      </div>
    </div>
    <div>
      <div>
        <div></div>
        <div></div>
      </div>
      <div>
        <div></div>
        <div></div>
      </div>
    </div>
  </div>
  <div>
    <div>
      <div>
        <div></div>
        <div></div>
      </div>
      <div>
        <div></div>
        <div></div>
      </div>
    </div>
    <div>
      <div>
        <div></div>
        <div></div>
      </div>
      <div>
        <div></div>
        <div></div>
      </div>
    </div>
  </div>
</div>
<div class="btn-wrap">
  <button>前序</button>
  <button>中序</button>
  <button>后序</button>
</div>
<script>
  let wrap = document.querySelector(".wrap");
  let btn_wrap = document.querySelector(".btn-wrap");
  let btn1 = btn_wrap.querySelectorAll("button")[0];
  let btn2 = btn_wrap.querySelectorAll("button")[1];
  let btn3 = btn_wrap.querySelectorAll("button")[2];
  let arr = [];
  let last;
  let toggle = false;
  //给按钮绑定事件
  btn1.onclick = function () {
    if (!toggle) {
      toggle = true;
      reset();
      preOrder(wrap);
      showWay();
    }
  };
  btn2.onclick = function () {
    if (!toggle) {
      toggle = true;
      reset();
      inOrder(wrap);
      showWay();
    }
  };
  btn3.onclick = function () {
    if (!toggle) {
      toggle = true;
      reset();
      postOrder(wrap);
      showWay();
    }
  };
  //二叉树的遍历的三种方式
  //(1)前序遍历（DLR)
  function preOrder(node) {
    if (node) {
      arr.push(node);
      preOrder(node.firstElementChild);
      preOrder(node.lastElementChild);
    }
  }

  //(2)中序遍历（LDR）
  function inOrder(node) {
    if (node) {
      inOrder(node.firstElementChild);
      arr.push(node);
      inOrder(node.lastElementChild);
    }
  }

  //(3)后序遍历（LRD）
  function postOrder(node) {
    if (node) {
      postOrder(node.firstElementChild);
      postOrder(node.lastElementChild);
      arr.push(node);
    }
  }

  //显示遍历的过程
  function showWay() {
    for (let i = 0; i < arr.length; i++) {
      setTimeout(function (i) {
        return function () {
          if (i === arr.length - 1) {
            toggle = false;
          }
          if (last) {
            last.style.background = "white";
          }
          arr[i].style.background = "red";
          last = arr[i];
        }
      }(i), i * 1000)
    }
  }

  //初始化
  function reset() {
    arr = [];
    if (last) {
      last.style.background = "white";
    }
  }
</script>
</body>
</html>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/05/15/树/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/05/15/树/" itemprop="url">树</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-15T17:11:36+08:00">
                2019-05-15
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/05/15/树/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/05/15/树/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/05/15/树/" class="leancloud_visitors" data-flag-title="树">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.6k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  7
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="树-tree"><a href="#树-tree" class="headerlink" title="树 tree"></a>树 tree</h1><ul>
<li>树（tree）是n（n&gt;=0）个节点的有限集合。</li>
<li>当 n=0 时，称为空树</li>
<li>在任意一个非空树中有如下特点<ul>
<li>有且仅有一个特定的称为 <strong>根（root）</strong> 的节点</li>
<li>当 n&gt;1 时，其余节点可分为m（m&gt;0）个互不相交的有限集，每一个集合本身又是一个树，称为根的 <strong>子树</strong></li>
<li>节点的末端，叫做 <strong>叶子节点（leaf）</strong></li>
</ul>
</li>
</ul>
<p><img src="tree.png" alt="例图"></p>
<h2 id="二叉树-binary-tree"><a href="#二叉树-binary-tree" class="headerlink" title="二叉树 binary tree"></a>二叉树 binary tree</h2><ul>
<li>树的一种特殊结构</li>
<li>每个节点最多有两个子节点</li>
<li>二叉树节点的两个孩子节点，一个被称为 <strong>左孩子（left child）</strong>，一个被称为 <strong>右孩子（right child）</strong><ul>
<li>这两个孩子顺序固定，不可颠倒</li>
</ul>
</li>
<li>存储结构<ul>
<li>链式存储结构</li>
<li>数组</li>
</ul>
</li>
</ul>
<h3 id="满二叉树"><a href="#满二叉树" class="headerlink" title="满二叉树"></a>满二叉树</h3><ul>
<li>一个二叉树的所有非叶子节点都存在左右孩子</li>
<li>所有叶子节点都在同一层级上</li>
</ul>
<h3 id="完全二叉树"><a href="#完全二叉树" class="headerlink" title="完全二叉树"></a>完全二叉树</h3><ul>
<li>对一个有 n 个节点的二叉树，按层级顺序编号，则所有节点的编号为从 1 到 n 。如果这个树所有节点和同样深度的满二叉树的编号为从 1 到 n 的节点位置相同，则这个二叉树为完全二叉树</li>
</ul>
<h3 id="二叉查找树"><a href="#二叉查找树" class="headerlink" title="二叉查找树"></a>二叉查找树</h3><ul>
<li>也叫二叉排序树</li>
<li>如果左子树不为空，则左子树上所有节点的值均小于根节点的值</li>
<li>如果右子树不为空，则右子树上所有节点的值均大于根节点的值</li>
<li>左右子树也是二叉树查找</li>
<li>对于一个 <strong>节点分布相对均衡</strong> 的二叉查找树来说，如果节点总数是 <strong>n</strong>，那么搜索节点的时间复杂度为 <strong>O(logn)</strong>,和数的深度一样</li>
<li>二叉树的自平衡</li>
</ul>
<h3 id="二叉树与存储结构"><a href="#二叉树与存储结构" class="headerlink" title="二叉树与存储结构"></a>二叉树与存储结构</h3><h4 id="链式存储结构"><a href="#链式存储结构" class="headerlink" title="链式存储结构"></a>链式存储结构</h4><p>二叉树的每一个节点由三部分组成</p>
<ul>
<li>存储数据的 data 变量</li>
<li>指向左孩子的 left 指针</li>
<li>指向右孩子的 right 指针</li>
</ul>
<h4 id="数组存储结构"><a href="#数组存储结构" class="headerlink" title="数组存储结构"></a>数组存储结构</h4><ul>
<li>按照层级顺序把二叉树的节点放到数组的对应位置上</li>
<li>如果某一节点的左孩子或右孩子空缺，则数组的相应位置也空出来</li>
<li>假设一个父节点的下标为 <strong>parent</strong>，那么它的左孩子的下标为 <strong>2 * parent + 1</strong>，它的右孩子的下标为 <strong>2 * parent + 2</strong></li>
<li>假设一个左孩子的下标为 <strong>leftChilde</strong> ，那么他的父节点的下标为 <strong>(leftChild - 1) / 2</strong></li>
</ul>
<h3 id="应用"><a href="#应用" class="headerlink" title="应用"></a>应用</h3><h4 id="二叉查找树（binary-search-tree）"><a href="#二叉查找树（binary-search-tree）" class="headerlink" title="二叉查找树（binary search tree）"></a>二叉查找树（binary search tree）</h4><p>二叉查找树在二叉树的基础上增加了以下条件</p>
<ul>
<li>如果左子树不为空，则左子树上所有节点的值均小于根节点的值</li>
<li>如果右子树不为空，则右子树上所有节点的值均大于根节点的值</li>
<li>左右子叶也是二叉树查找</li>
<li>时间复杂度 O(logn), n 为树的深度</li>
</ul>
<h4 id="维持相对顺序"><a href="#维持相对顺序" class="headerlink" title="维持相对顺序"></a>维持相对顺序</h4><ul>
<li>二叉查找树的左子树小于父节点，右子树大于父节点，保证了二叉树的有序性</li>
</ul>
<h3 id="二叉树遍历"><a href="#二叉树遍历" class="headerlink" title="二叉树遍历"></a>二叉树遍历</h3><p>将 <strong>非线性的数据结构</strong> 转换成 <strong>线性的序列</strong></p>
<h4 id="深度优先遍历-demo"><a href="#深度优先遍历-demo" class="headerlink" title="深度优先遍历 (demo)"></a>深度优先遍历 (<a href="demo.html">demo</a>)</h4><p>偏向于深度，<strong>一头扎到底</strong>的访问方式</p>
<h5 id="前序遍历（DLR"><a href="#前序遍历（DLR" class="headerlink" title="前序遍历（DLR)"></a>前序遍历（DLR)</h5><ul>
<li>过程</li>
</ul>
<ol>
<li>先访问 <strong>根节点</strong></li>
<li>遍历 <strong>左子树</strong></li>
<li>遍历 <strong>右子树</strong></li>
</ol>
<ul>
<li>结果 : ABDECF</li>
</ul>
<h5 id="中序遍历（LDR）"><a href="#中序遍历（LDR）" class="headerlink" title="中序遍历（LDR）"></a>中序遍历（LDR）</h5><ul>
<li>过程<br>左 根 右</li>
<li>结果 : DBEACF</li>
</ul>
<h5 id="后序遍历（LRD）"><a href="#后序遍历（LRD）" class="headerlink" title="后序遍历（LRD）"></a>后序遍历（LRD）</h5><ul>
<li>过程<br>左 右 根</li>
<li>结果 ： DEBFCA</li>
</ul>
<h5 id="demo"><a href="#demo" class="headerlink" title="demo"></a>demo</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Binary Tree<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.root</span> &#123;</span></span><br><span class="line"><span class="undefined">      display: flex;</span></span><br><span class="line"><span class="css">      <span class="selector-tag">border</span>: 1<span class="selector-tag">px</span> <span class="selector-tag">solid</span> <span class="selector-id">#000</span>;</span></span><br><span class="line"><span class="undefined">      width: 600px;</span></span><br><span class="line"><span class="undefined">      margin: 0 auto;</span></span><br><span class="line"><span class="undefined">      height: 150px;</span></span><br><span class="line"><span class="undefined">      align-items: center;</span></span><br><span class="line"><span class="undefined">      justify-content: center;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.root</span> <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="undefined">      display: flex;</span></span><br><span class="line"><span class="undefined">      height: 70%;</span></span><br><span class="line"><span class="undefined">      width: 44%;</span></span><br><span class="line"><span class="undefined">      margin: 0 3%;</span></span><br><span class="line"><span class="css">      <span class="selector-tag">border</span>: 1<span class="selector-tag">px</span> <span class="selector-tag">solid</span> <span class="selector-id">#000</span>;</span></span><br><span class="line"><span class="undefined">      justify-content: center;</span></span><br><span class="line"><span class="undefined">      align-items: center;</span></span><br><span class="line"><span class="css">      <span class="selector-tag">background</span>: <span class="selector-id">#fff</span>;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">  </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"root"</span>&gt;</span></span><br><span class="line">  1</span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>2</span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>4<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>5<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>3</span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>6<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>7<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined">  /**</span></span><br><span class="line"><span class="undefined">   * 深度优先遍历的递归写法</span></span><br><span class="line"><span class="undefined">   * 原理：递归的回溯性</span></span><br><span class="line"><span class="undefined">   */</span></span><br><span class="line"><span class="actionscript">  <span class="class"><span class="keyword">class</span> <span class="title">TravelTree</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">constructor</span>() &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.nodeList = [];</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">    preOrder = <span class="function"><span class="params">node</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">if</span> (node) &#123;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.nodeList.push(node.firstChild.textContent.trim());</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.preOrder(node.firstElementChild);</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.preOrder(node.lastElementChild);</span></span><br><span class="line"><span class="undefined">      &#125;</span></span><br><span class="line"><span class="undefined">    &#125;;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">    inOrder = <span class="function"><span class="params">node</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">if</span> (node) &#123;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.inOrder(node.firstElementChild);</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.nodeList.push(node.firstChild.textContent.trim());</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.inOrder(node.lastElementChild);</span></span><br><span class="line"><span class="undefined">      &#125;</span></span><br><span class="line"><span class="undefined">    &#125;;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">    postOrder = <span class="function"><span class="params">node</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">if</span> (node) &#123;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.postOrder(node.firstElementChild);</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.postOrder(node.lastElementChild);</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.nodeList.push(node.firstChild.textContent.trim());</span></span><br><span class="line"><span class="undefined">      &#125;</span></span><br><span class="line"><span class="undefined">    &#125;;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    reset() &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.nodeList = [];</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">let</span> node = <span class="built_in">document</span>.querySelector(<span class="string">'.root'</span>);</span></span><br><span class="line"><span class="javascript">  <span class="keyword">let</span> tree = <span class="keyword">new</span> TravelTree();</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(<span class="string">'TravelTree'</span>);</span></span><br><span class="line"><span class="undefined">  tree.reset();</span></span><br><span class="line"><span class="undefined">  tree.preOrder(node);</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(tree.nodeList);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  tree.reset();</span></span><br><span class="line"><span class="undefined">  tree.inOrder(node);</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(tree.nodeList);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  tree.reset();</span></span><br><span class="line"><span class="undefined">  tree.postOrder(node);</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(tree.nodeList);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="actionscript">  <span class="comment">// 深度优先遍历的非递归写法</span></span></span><br><span class="line"><span class="actionscript">  <span class="class"><span class="keyword">class</span> <span class="title">StackTree</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">constructor</span>() &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.nodeList = [];</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.stack = []</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    preOrder(node) &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">let</span> treeNode = node;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">while</span> (treeNode || <span class="keyword">this</span>.stack.length) &#123;</span></span><br><span class="line"><span class="actionscript">        <span class="comment">// 进栈 将所有左节点压栈</span></span></span><br><span class="line"><span class="actionscript">        <span class="keyword">while</span> (treeNode) &#123;</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">this</span>.stack.push(treeNode);</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">this</span>.nodeList.push(treeNode.firstChild.textContent.trim());</span></span><br><span class="line"><span class="undefined">          treeNode = treeNode.firstElementChild;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="actionscript">        <span class="comment">// 出栈</span></span></span><br><span class="line"><span class="actionscript">        <span class="keyword">if</span> (<span class="keyword">this</span>.stack.length) &#123;</span></span><br><span class="line"><span class="actionscript">          treeNode = <span class="keyword">this</span>.stack.pop();</span></span><br><span class="line"><span class="undefined">          treeNode = treeNode.lastElementChild;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined">      &#125;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    inOrder(node) &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">let</span> treeNode = node;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">while</span> (treeNode || <span class="keyword">this</span>.stack.length) &#123;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">while</span> (treeNode) &#123;</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">this</span>.stack.push(treeNode);</span></span><br><span class="line"><span class="undefined">          treeNode = treeNode.firstElementChild;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">if</span> (<span class="keyword">this</span>.stack.length) &#123;</span></span><br><span class="line"><span class="actionscript">          treeNode = <span class="keyword">this</span>.stack.pop();</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">this</span>.nodeList.push(treeNode.firstChild.textContent.trim());</span></span><br><span class="line"><span class="undefined">          treeNode = treeNode.lastElementChild;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined">      &#125;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    /*</span></span><br><span class="line"><span class="undefined">    *  将根节点压入第一个栈</span></span><br><span class="line"><span class="undefined">    *  从第一个栈中弹出一个元素，压入第二个栈</span></span><br><span class="line"><span class="undefined">    *  然后分别将该节点的左右孩子压入第一个栈</span></span><br><span class="line"><span class="undefined">    *  重复步骤2和步骤3直到第一个栈为空</span></span><br><span class="line"><span class="undefined">    *  执行结束，第二个栈中就保存了所有节点的后序遍历输出结果。依次将元素从第二个栈中弹出即可。</span></span><br><span class="line"><span class="undefined">    */</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    postOrder(node) &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">let</span> treeNode = node;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.stack.push(treeNode);</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">while</span> (<span class="keyword">this</span>.stack.length) &#123;</span></span><br><span class="line"><span class="actionscript">        treeNode = <span class="keyword">this</span>.stack.pop();</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.nodeList.unshift(treeNode.firstChild.textContent.trim());</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="actionscript">        <span class="keyword">if</span> (treeNode.firstElementChild) &#123;</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">this</span>.stack.push(treeNode.firstElementChild)</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">if</span> (treeNode.lastElementChild) &#123;</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">this</span>.stack.push(treeNode.lastElementChild)</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined">      &#125;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">    </span></span><br><span class="line"><span class="actionscript">    <span class="comment">// todo: 单个栈的实现方法</span></span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    reset() &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.stack = [];</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.nodeList = [];</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">let</span> stackTree = <span class="keyword">new</span> StackTree();</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(<span class="string">'stackTree'</span>);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  stackTree.preOrder(node);</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(stackTree.nodeList);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  stackTree.reset();</span></span><br><span class="line"><span class="undefined">  stackTree.inOrder(node);</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(stackTree.nodeList);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  stackTree.reset();</span></span><br><span class="line"><span class="undefined">  stackTree.postOrder(node);</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(stackTree.nodeList);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="actionscript">  <span class="comment">// 广度度优先遍历非递归</span></span></span><br><span class="line"><span class="actionscript">  <span class="class"><span class="keyword">class</span> <span class="title">WidthTravelTree</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">constructor</span>() &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.nodeList = [];</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.stack = []</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="actionscript">    <span class="comment">// 队列</span></span></span><br><span class="line"><span class="undefined">    widthTravel(node) &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">if</span> (node) &#123;</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.stack.push(node);</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">this</span>.nodeList.push(node.firstChild.textContent.trim());</span></span><br><span class="line"><span class="actionscript">        <span class="keyword">while</span> (<span class="keyword">this</span>.stack.length) &#123;</span></span><br><span class="line"><span class="javascript">          <span class="keyword">let</span> item = <span class="keyword">this</span>.stack.shift();</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">if</span> (item.firstElementChild) &#123;</span></span><br><span class="line"><span class="actionscript">            <span class="keyword">this</span>.nodeList.push(item.firstElementChild.firstChild.textContent.trim());</span></span><br><span class="line"><span class="actionscript">            <span class="keyword">this</span>.stack.push(item.firstElementChild);</span></span><br><span class="line"><span class="undefined">          &#125;</span></span><br><span class="line"><span class="actionscript">          <span class="keyword">if</span> (item.lastElementChild) &#123;</span></span><br><span class="line"><span class="actionscript">            <span class="keyword">this</span>.nodeList.push(item.lastElementChild.firstChild.textContent.trim());</span></span><br><span class="line"><span class="actionscript">            <span class="keyword">this</span>.stack.push(item.lastElementChild)</span></span><br><span class="line"><span class="undefined">          &#125;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined">      &#125;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">  &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(<span class="string">'WidthTravelTree'</span>);</span></span><br><span class="line"><span class="javascript">  <span class="keyword">let</span> widthTravelTree = <span class="keyword">new</span> WidthTravelTree();</span></span><br><span class="line"><span class="undefined">  widthTravelTree.widthTravel(node);</span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(widthTravelTree.nodeList);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="广度优先遍历"><a href="#广度优先遍历" class="headerlink" title="广度优先遍历"></a>广度优先遍历</h4><ul>
<li>层序遍历<br><img src="each.png" alt="图">  </li>
</ul>
<h2 id="资料参考"><a href="#资料参考" class="headerlink" title="资料参考"></a>资料参考</h2><ul>
<li><a href="https://blog.51cto.com/9291927/2068745" target="_blank" rel="noopener">图片来源</a></li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.bevexed.top/2019/05/14/数据结构基础/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="两万">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/IMG_2021.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="两万">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/05/14/数据结构基础/" itemprop="url">数据结构基础</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-14T17:14:47+08:00">
                2019-05-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/05/14/数据结构基础/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/05/14/数据结构基础/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2019/05/14/数据结构基础/" class="leancloud_visitors" data-flag-title="数据结构基础">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><ul>
<li>数据的组织、管理和存储格式</li>
<li>提高数据的访问和修改效率<h3 id="分类"><a href="#分类" class="headerlink" title="分类"></a>分类</h3></li>
<li><p>线性结构</p>
<ul>
<li>数组、链表</li>
<li>栈、对、列、哈希表</li>
</ul>
</li>
<li><p>树</p>
<ul>
<li>二叉树</li>
<li>二叉堆</li>
</ul>
</li>
<li><p>图</p>
</li>
<li>其他数据结构<ul>
<li>跳表、哈希链表、位图、散列表</li>
</ul>
</li>
</ul>
<h2 id="数组-array"><a href="#数组-array" class="headerlink" title="数组 array"></a>数组 <code>array</code></h2><ul>
<li>有限个相同类型变量组成的有序集合</li>
<li>数组中的每一个变量被称为元素</li>
<li>数组是最简单最为常用的数据结构</li>
<li>在内存中顺序存储</li>
<li>读取数组元素和更新数组元素的时间复杂度都是 O(1)</li>
<li>数组的插入和删除操作的时间复杂度为 O(n)</li>
<li>适用于读操作多，写操作少的情景</li>
</ul>
<h2 id="链表-link-list"><a href="#链表-link-list" class="headerlink" title="链表 link list"></a>链表 <code>link list</code></h2><ul>
<li>链表是一种物理上非连续、非顺序的数据结构，由若干个节点(node)组成</li>
<li>链表中的第一个节点叫做节点头，最后一个节点被称为节点尾，尾节点的 next 指针指向空</li>
<li>在内存中随机存储</li>
<li>链表查询节点的最坏时间复杂度为 O(n)</li>
<li>链表更新、插入、删除节点的时间复杂度为 O(1)<br>– 适用于写操作多，读操作少的情景<h3 id="单向链表"><a href="#单向链表" class="headerlink" title="单向链表"></a>单向链表</h3></li>
<li>单向链表的每一个节点包涵两部分<ul>
<li>一部分存放数据变量data</li>
<li>另一部分是指向下一个节点的指针 next</li>
</ul>
</li>
<li>一级一级，单线传递<h4 id="双向链表"><a href="#双向链表" class="headerlink" title="双向链表"></a>双向链表</h4></li>
<li>每一个节点都拥有 data，next指针 和 prev指针</li>
<li>双向传递</li>
</ul>
<h2 id="栈-stack"><a href="#栈-stack" class="headerlink" title="栈 stack"></a>栈 <code>stack</code></h2><ul>
<li>一种线性数据结构</li>
<li>栈中元素只能先入后出（First In Last Out，简称 FILO）</li>
<li>最早进入的元素存放的位置叫栈底（bottom）</li>
<li>最后进入的元素存放的位置叫栈顶（top）</li>
<li>数据结构即可以用数组实现，也可以用链表实现<h3 id="操作"><a href="#操作" class="headerlink" title="操作"></a>操作</h3></li>
<li>入栈<ul>
<li>入栈操作（push）是把新元素放入栈中</li>
<li>只允许从栈顶一侧放入元素</li>
<li>新元素的位置成为新的栈顶</li>
</ul>
</li>
<li>出栈<ul>
<li>出栈操作（pop）是把元素从栈中弹出</li>
<li>只有栈顶元素才允许出栈</li>
<li>出栈元素的前一个元素会成为新的栈顶</li>
</ul>
</li>
<li>出栈和入栈的时间复杂度都是 O(1)<h3 id="应用"><a href="#应用" class="headerlink" title="应用"></a>应用</h3></li>
<li>历史回溯</li>
<li>面包屑导航</li>
</ul>
<h2 id="队列-queue"><a href="#队列-queue" class="headerlink" title="队列 queue"></a>队列 <code>queue</code></h2><ul>
<li>一种线性的数据结构</li>
<li>队列中的元素先入先出（First In First Out，简称 FIFO）</li>
<li>队列的出口端叫做队头（front）</li>
<li>队列的入口端叫做队尾（rear）<h3 id="操作-1"><a href="#操作-1" class="headerlink" title="操作"></a>操作</h3></li>
<li>入队<ul>
<li>入队（queue）是把新元素放入队列中</li>
<li>只允许在队尾的位置放置新元素</li>
<li>新元素的下一个位置将会成为下一个队尾</li>
</ul>
</li>
<li>出队<ul>
<li>出队（dequeue）是把元素移出队列，</li>
<li>只允许在队头一侧移出元素</li>
<li>出队元素后的一个元素将成为队头</li>
<li>队列循环</li>
</ul>
</li>
<li>出队和入队的时间复杂度都是 O(1)<h3 id="应用-1"><a href="#应用-1" class="headerlink" title="应用"></a>应用</h3></li>
<li>多线程，争夺公平锁的等待队列</li>
</ul>
<h2 id="双端队列-deque"><a href="#双端队列-deque" class="headerlink" title="双端队列 deque"></a>双端队列 <code>deque</code></h2><ul>
<li>综合栈和队列的优缺点</li>
</ul>
<h2 id="优先队列"><a href="#优先队列" class="headerlink" title="优先队列"></a>优先队列</h2><ul>
<li>基于二叉堆实现</li>
</ul>
<h2 id="散列表"><a href="#散列表" class="headerlink" title="散列表"></a>散列表</h2><ul>
<li>也叫哈希表（hash table）</li>
<li>提供了键（key）和值（value）的映射关系</li>
<li>只要给出一个 key,就可以高效的查找他说匹配的 Value，时间复杂度接近于 O(1)</li>
<li>本质是数组，通过哈希函数转换<h3 id="操作-2"><a href="#操作-2" class="headerlink" title="操作"></a>操作</h3></li>
<li><p>写操作（put）</p>
<ol>
<li>通过哈希函数，把 key 转换为数值下标</li>
<li>如果下标对应的位置没有元素，将键值对插入到当前位置</li>
<li>当下标对应的位置已经存在元素，就会出现<code>哈希冲突</code><ul>
<li>哈希冲突的主要解决方式是<code>开放寻址法</code>和<code>链表法</code></li>
</ul>
</li>
</ol>
</li>
<li><p>读操作（get）</p>
<ol>
<li>通过哈希函数，把 key 转换为数值下标</li>
<li>通过在链表内匹配 key 值，寻找节点</li>
</ol>
</li>
<li><p>扩容（resize）</p>
<ul>
<li>原因<ol>
<li>当经过多次元素插入，散列表达到一定饱和度时，key 映射位置发生冲突的概率会逐渐提高</li>
<li>大量元素拥挤在相同的数组下标位置，会形成很长的链表，对后续的读写抄作有很大的性能影响</li>
<li>这时，散列表需要扩展他的长度</li>
</ol>
</li>
<li>过程<ol>
<li>扩容，创建一个新的 Entry 空数组，长度是原数组的2倍</li>
<li>重新Hash，遍历原 Entry 数组，把所有的 Entry 重新 Hash 到新数组中 </li>
</ol>
</li>
</ul>
</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="page-number current">2</span><a class="page-number" href="/page/3/">3</a><span class="space">&hellip;</span><a class="page-number" href="/page/5/">5</a><a class="extend next" rel="next" href="/page/3/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/IMG_2021.jpg"
                alt="两万" />
            
              <p class="site-author-name" itemprop="name">两万</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">41</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/bevexed" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://cn.vuejs.org/" title="Vue" target="_blank">Vue</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://bevexed.top/frontEnd/H5_Daily/canvas/demo/FlappyBird" title="FlapBird" target="_blank">FlapBird</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">两万</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">26.6k</span>
  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://.disqus.com/count.js" async></script>
    

    

  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: true,
        notify: true,
        appId: 'EoJiOrKeNWgSwq0eXmK0VK7V-gzGzoHsz',
        appKey: 'H1riqYCRgB88YzWIOinxfaAg',
        placeholder: '说点啥吧......',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("EoJiOrKeNWgSwq0eXmK0VK7V-gzGzoHsz", "H1riqYCRgB88YzWIOinxfaAg");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=5.1.4"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=5.1.4"></script>


  
  <script type="text/javascript" src="/js/src/exturl.js?v=5.1.4"></script>


</body>
</html>
